package com.aaa.backsystem.controller;

import com.aaa.backsystem.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

/**
 * fileName:LoginController
 * description:
 * author:gyc
 * createTime:2020/7/20 20:29
 * version:1.0.0
 */
@Controller
public class LoginController {

    @Autowired
    private UserService userService;

    @RequestMapping("/toLogin")
    public String toLogin(){
        return "login";
    }

    @RequestMapping("/login")
    public String login(String userName, String password, Model model, HttpSession session){
       if(userName!=null && password !=null){
           AuthenticationToken token = new UsernamePasswordToken(userName,password);
           Subject subject = SecurityUtils.getSubject();
           try {
               subject.login(token);
           } catch (AuthenticationException e) {

               if(e.getMessage().equals("账号异常")){
                   model.addAttribute("message",e.getMessage());
               }else{
                   model.addAttribute("message","账号密码错误");
               }

               return "login";
           }
           //用户信息传到前端
           model.addAttribute("userInfo",subject.getPrincipal());
           return "index";
       }
        return "login";
    }

    /**
     * 退出登录
     */
    @RequestMapping("/logout")
    public String logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "login";
    }

}
